package targaman.moltenwow.autovoter.components;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import targaman.moltenwow.autovoter.Logger.MoltenLogger;
import targaman.moltenwow.autovoter.lang.Constants;
import targaman.moltenwow.autovoter.services.VerifyMoltenConnectivity;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by Jonathan on 26/12/2014.
 */
@Component(value = Constants.VERIFY_MOLTEN_CONNETIVITY_SERVICE)
public class VerifyMoltenConnectivityImpl implements VerifyMoltenConnectivity {

     private InetAddress address;

   

    @Autowired
    @Qualifier(value = Constants.MOLTEN_HOST_NAME)
    private String moltenHostName;

    @Override
    public boolean canConnect() {
        try {
            address = InetAddress.getByName(moltenHostName);

            if(!address.getHostName().equals("localhost")) {
                MoltenLogger.Log(Constants.INTERNET_CONNECTION_TO_MOLTEN_WO_W_AVAILABLE);
                return true;
            }
            MoltenLogger.Log(Constants.INTERNET_CONNECTION_TO_MOLTEN_WO_W_FAILED);
            return false;
        } catch (UnknownHostException e) {
            MoltenLogger.Log(Constants.INTERNET_CONNECTION_TO_MOLTEN_WO_W_FAILED);
            return false;
        }
    }

    public String getMoltenHostName() {
        return moltenHostName;
    }

    public void setMoltenHostName(String moltenHostName) {
        this.moltenHostName = moltenHostName;
    }
}
